package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.entity.Result;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public Result submit(OrderDto orderDto) {

        //根据日期查询，查看当天是否还有预约数量
        OrderSetting orderSetting = orderSettingMapper.selectOne(Wrappers.<OrderSetting>lambdaQuery()
                .eq(OrderSetting::getOrderDate, DateUtils.dateToString(orderDto.getOrderDate(),"yyyy-MM-dd")));
        //查看当天是否可以预约
        if(orderSetting == null){
            return new Result(false,"今天不能预约，请重新选择其他时间" ,null);
        }
        //查看当前是否约满
        if(orderSetting.getNumber() == orderSetting.getReservations()){
            return new Result(false,"今天已经约满，请重新选择其他时间" ,null);
        }
        //把剩余数量加+1
        orderSetting.setReservations(orderSetting.getReservations()+1);
        //更新预约剩余数量
        orderSettingMapper.updateById(orderSetting);


        //把orderDto复制为order对象
        Order order = new Order();
        //获取member的id 并补全参数到order
        order.setMemberId(orderDto.getMemberId());
        order.setOrderDate(orderDto.getOrderDate());  //预约时间
        order.setOrderType(Order.ORDERTYPE_WEIXIN);  //微信预约
        order.setOrderStatus(Order.ORDERSTATUS_NO);  //未到诊
        order.setSetmealId(orderDto.getSetmealId()); //预约套餐Id
        orderMapper.insert(order);

        //创建member对象 并封装对应数据到数据库
        Member member = new Member();
        member.setId(order.getMemberId());
        member.setPhoneNumber(orderDto.getTelephone());  //会员手机号
        member.setIdCard(orderDto.getIdCard());  //会员身份证
        member.setSex(orderDto.getSex());  //会员性别
        member.setName(orderDto.getName());  //会员姓名
        memberMapper.updateById(member);

        return new Result(true,"预约成功" ,order.getId());
    }

    @Override
    public Result findById(Integer id) {
        Order order = orderMapper.selectById(id);
        OrderSuccess orderSuccess = new OrderSuccess();
        LambdaQueryWrapper<Member> memberWrapper = new LambdaQueryWrapper<>();
        memberWrapper.eq(Member::getId,order.getMemberId() );
        Member member = memberMapper.selectOne(memberWrapper);
        orderSuccess.setMember(member.getName());
        orderSuccess.setOrderDate(order.getOrderDate());
        orderSuccess.setOrderType(order.getOrderType());
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getId,order.getSetmealId() );
        Setmeal setmeal = setmealMapper.selectOne(setmealWrapper);

        orderSuccess.setSetmeal(setmeal.getName());

        return new Result(true,"查询预约信息成功",orderSuccess);
    }
}
